package com.example.drawwave;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.LinearInterpolator;

public class MyWaveView extends View {
    private final String TAG = "MyWaveView";
    private Paint mPaint;

    //View宽度
    private int width;
    //View高度
    private int height;

    //波浪高低偏移量
    private int offset = 100;

    //X轴，View的偏移量
    private float xoffset = 0;

    //View的Y轴高度
    private int viewY = 0;

    //波浪速度
    private int waveSpeed = 50;

    private ValueAnimator valueAnimator;


    public MyWaveView(Context context) {
        super(context);
        init(context);
    }

    public MyWaveView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void  init(Context context){
        mPaint = new Paint();

        WindowManager windowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        width = windowManager.getDefaultDisplay().getWidth();
        height = windowManager.getDefaultDisplay().getHeight();

        valueAnimator = new ValueAnimator();
        valueAnimator.setFloatValues(0,width);
        valueAnimator.setDuration(waveSpeed*20);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.setRepeatCount(-1);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float change = (float) animation.getAnimatedValue();
                xoffset = (int) change;
                createShader();
                invalidate();
            }
        });
        valueAnimator.start();

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
        //Log.i(TAG, "onMeasure: ");
    }

    private int measureWidth(int measureSpec){
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        //Log.i(TAG, "measureWidth: "+measureSpec);
        //设置一个默认值，就是这个View 的默认宽度为500，这个看我们自定义View的要求
        int result = 500;
        if (specMode == MeasureSpec.AT_MOST){
            //相当与我们设置为wrap_content
            result = specSize;
        }else if (specMode == MeasureSpec.EXACTLY){
            //相当于我们设置为match_parent或者为一个具体的值
            result = specSize;
        }
        width = result;
        return result;
    }

    private int measureHeight(int measureSpec){
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        //Log.i(TAG, "measureHeight: "+measureSpec);
        int result = 500;
        if (specMode == MeasureSpec.AT_MOST){
            result = specSize;
        }else if (specMode == MeasureSpec.EXACTLY){
            result = specSize;
        }
        height = result;//---
        return result;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawRect(new Rect(200,0,width-200,height),mPaint);

    }

    private void createShader(){
        //Log.i("TAG", "createShader: width:"+width+" height:"+height);
        Bitmap bitmap = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);

        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.RED);
        //paint.setAntiAlias(true);
        paint.setStrokeWidth(5);
        paint.setStyle(Paint.Style.FILL);

        Path path = new Path();
        viewY = height / 2;
        //Log.i(TAG, "createShader: offset "+offset);

        path.moveTo(xoffset-width,viewY);//xoffset最大值为width，也就是说，当xoffset为width时，path.moveTo(0,viewY)
        //
        int waveCount = 3;//一个屏幕内波浪个数（一个波浪有上下波）
        int screenCount = 2; //这里屏幕数2就行，当path.moveTo(0,viewY)时，还有一个屏幕的波浪显示
        for (int i=0;i<screenCount;i++){//两个屏幕的波浪
            // rQuadTo 和 quadTo 区别在于
            // rQuadTo 是相对上一个点 而 quadTo是相对于画布
            //offset波浪高度
            //waveWidth
            path.rQuadTo(width/4,-offset,width/2,0);
            path.rQuadTo(width/4,offset,width/2,0);
        }
        //新增
        path.rLineTo(0,height);
        path.rLineTo(-2*width,0);
        path.close();

        //paint.setStyle(Paint.Style.STROKE);
        canvas.drawPath(path,paint);
        BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        mPaint.setShader(bitmapShader);
    }

    /**
     * 设置波浪高度
     * @param waveHeight
     */
    public void setWaveHeight(int waveHeight){
        this.offset = waveHeight;
    }

    /**
     * 设置波浪速度
     * @param speed
     */
    public void setWaveSpeed(int speed){
        waveSpeed = 2000 - speed*20;
        valueAnimator.setDuration(waveSpeed);
    }

    /**
     * 获取波浪速度
     * @return
     */
    public int getWaveSpeed(){
        return  waveSpeed;
    }

    public MyWaveView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyWaveView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }
}
